package com.ruoyi.service.jpjs.bi_operate.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mapper.jpjs.bi_operate.OperateMapper;
import com.ruoyi.service.jpjs.bi_operate.OperateService;
import com.ruoyi.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OperateServiceImpl implements OperateService {

    @Autowired
    private OperateMapper operateMapper;


    @Override
    public List<Map<String, Object>> selectOperateList(Map<String, Object> map) {
        return operateMapper.selectOperateList(map);
    }

    @Override
    public Map<String, Object> selectOperate(String id) {
        return operateMapper.selectOperate(id);
    }

    @Override
    public int addOperate(Map<String, Object> map) {
        return operateMapper.addOperate(map);
    }

    @Override
    public int editOperate(Map<String, Object> map) {
        return operateMapper.editOperate(map);
    }

    @Override
    public int deleteOperate(String id) {
        return operateMapper.deleteOperate(id);
    }

    @Override
    public int importData(MultipartFile file) {
        String[] colNames = new String[]{"compName", "proName", "years", "months", "type", "riskLevel", "riskExplain", "riskReason"};
        List<Map<String, Object>> list = ExcelUtils.getExcelData(file, colNames);
        if (list.size() > 0) {
            int endIndex = 100;
            for (int i = 0; i < list.size(); i = endIndex + 1, endIndex += 100) {
                operateMapper.addImportOperate(list.subList(i, endIndex >= list.size() ? list.size() : endIndex + 1));
            }
        }
        return 1;
    }

    @Override
    public AjaxResult importTemplate() {
        List<String> colNameList = new ArrayList<String>();
        colNameList.add("工程公司");
        colNameList.add("项目名称");
        colNameList.add("年份");
        colNameList.add("月份");
        colNameList.add("异常类型");
        colNameList.add("风险等级");
        colNameList.add("风险说明");
        colNameList.add("解决措施");
        ExcelUtils.exportData(colNameList, new ArrayList<Map<String, Object>>(), "运营异常模板");
        return AjaxResult.success("运营异常模板.xls");
    }
}
